package com.yu.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yu.crm.domain.pojo.CrmDepartmentPositionLevel;
import com.yu.crm.mapper.CrmDepartmentPositionLevelMapper;
import com.yu.crm.service.CrmDepartmentPositionLevelService;
import com.yxzz.common.BaseServiceImpl;
import com.yxzz.common.helper.DataHelpers;
import com.yxzz.common.helper.DateHelpers;
import com.yxzz.common.helper.ExceptionHelpers;
import com.yxzz.common.helper.valid.ValidatorHelpers;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 部门职位级别表 服务实现类
 * </p>
 *
 * @author donnie-bin
 * @since 2019-06-10
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class CrmDepartmentPositionLevelServiceImpl extends BaseServiceImpl<CrmDepartmentPositionLevelMapper, CrmDepartmentPositionLevel> implements CrmDepartmentPositionLevelService {

    @Override
    public boolean saveOrUpdate(CrmDepartmentPositionLevel entity) {
        if(DataHelpers.isEmpty(entity.getId())){
            QueryWrapper<CrmDepartmentPositionLevel> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("position_level_name", entity.getPositionLevelName());
            int oldCount = super.count(queryWrapper);
            ValidatorHelpers.validateDataExist("级别名称不能重复", oldCount);
            entity.setGmtCreate(DateHelpers.now());
            entity.setGmtModified(DateHelpers.now());
            return ExceptionHelpers.checkData(super.save(entity), "新增失败");
        }else{
            entity.setGmtModified(DateHelpers.now());
            return ExceptionHelpers.checkData(super.updateById(entity), "修改失败");
        }
    }

    @Override
    public IPage<CrmDepartmentPositionLevel> list(IPage<CrmDepartmentPositionLevel> page, CrmDepartmentPositionLevel queryInfo) {
        QueryWrapper<CrmDepartmentPositionLevel> queryWrapper = new QueryWrapper<>();
        if(DataHelpers.isNotEmpty(queryInfo.getPositionLevelName())){
            queryWrapper.like("position_level_name", queryInfo.getPositionLevelName());
        }
        queryWrapper.orderByAsc("position_level_name");
        return super.page(page, queryWrapper);
    }
}
